/*异步测试包含：局部日志器、全局日志器的测试*/

#include "../../include/logger.hpp"


using namespace xzt::log;

void LocalLoggerTest()
{
    // 利用建造者模式创建日志对象
    std::unique_ptr<LoggerBulider> builder = std::make_unique<LocalLoggerBulider>();

    builder->bulidLoggerType(LoggerType::LOGGER_ASYNC);
    builder->bulidLoggerName("local-logger");
    builder->bulidLimitLevel(Level::value::DEBUG);
    // formatter 若不进行设置，则默认使用 DefaultPatternFormatter
    builder->bulidSink<StdoutSink>();
    builder->bulidSink<FileSink>("./logs/async_local_test.log");
    builder->bulidEnableAsyncUnSafe();  // 无限扩容
    Logger::ptr logger = builder->bulid();  // 构建日志对象

    for(int i = 0; i < 100000; ++i)
    {
        int idx = i;
        logger->debug(__FILE__, __LINE__, "Debug... - {%d}", idx++);
        logger->info(__FILE__, __LINE__, "Info... - {%d}", idx++);
        logger->warn(__FILE__, __LINE__, "Warn... - {%d}", idx++);
        logger->error(__FILE__, __LINE__, "Error... - {%d}", idx++);
        logger->fatal(__FILE__, __LINE__, "Fatal... - {%d}", idx++);
    }
}


// 全局日志器测试
// 模拟用户直接使用日志对象进行日志输出
void SinkMessage()
{
    xzt::log::LoggerManager::getInstance();
    Logger::ptr logger = xzt::log::LoggerManager::getInstance().getLogger("global-logger");
    if (logger == nullptr)
    {
        return;
    }

    for(int i = 0; i < 10000; ++i)
    {
        int idx = i;
        logger->debug(__FILE__, __LINE__, "Debug... - {%d}", idx++);
        logger->info(__FILE__, __LINE__, "Info... - {%d}", idx++);
        logger->warn(__FILE__, __LINE__, "Warn... - {%d}", idx++);
        logger->error(__FILE__, __LINE__, "Error... - {%d}", idx++);
        logger->fatal(__FILE__, __LINE__, "Fatal... - {%d}", idx++);
    }
}

void GlobalLoggerTest()
{
    // 利用建造者模式创建日志对象
    std::unique_ptr<LoggerBulider> builder = std::make_unique<GlobalLoggerBulider>();

    builder->bulidLoggerType(LoggerType::LOGGER_ASYNC);
    builder->bulidLoggerName("global-logger");
    builder->bulidLimitLevel(Level::value::DEBUG);
    // formatter 若不进行设置，则默认使用 DefaultPatternFormatter
    builder->bulidSink<StdoutSink>();
    builder->bulidSink<FileSink>("./logs/async_global_test.log");
    builder->bulidEnableAsyncUnSafe();  // 无限扩容
    builder->bulid();  // 在全局区构建单例日志对象

    SinkMessage();
}



int main() 
{
    // LocalLoggerTest();
    GlobalLoggerTest();
    return 0;
}